

<?php
require_once('Init.php');

/**
 * Description of CodeToGui
 *
 * @author Jens Beyer
 */
class CodeToGui
{
    protected $_at;

    protected $_wholeAjaxContent;

    protected $_fuctionData;

    protected $_sendedAjaxData;

    protected $_ajaxDataToSend;

    /**
     *
     * @var array
     */
    protected $_functionStartEndPoints;

    protected $_functionContent;


    public function mainRoutine()
    {
        $this->_functionCodeStartEndPoints = array();
        $this->_sendedAjaxData = file_get_contents('./GetFiles.php'); //$this->fetchDataFromAjax();
        if (empty($this->_sendedAjaxData)) {
            $this->getAt()->display(getDocumentRoot() . 'templates/CodeToGui.tpl');
            return false;
        } else {
            $this->_wholeAjaxContent = $this->_sendedAjaxData; //urldecode($this->_sendedAjaxData);
        }
        $this->putTextInToFile($this->_wholeAjaxContent, './CodeToSave.php');
        $function = 'getAllGaleriaFiles';
        $this->_functionContent[$function] = $this->grepFunctionText($function, './CodeToSave.php');
        $this->_functionAddedStaticCall[$function] = $this->addOnStaticCallInFunction(
                    $this->_functionContent, $function
                );
        $manipulatedFunctionContent =
            $this->replaceFunctionInCode(
                $this->_functionAddedStaticCall,
                $function,
                './CodeToSave.php'
            )
        ;
        file_put_contents('./manipulatedCode.php', $manipulatedFunctionContent);
        $this->appendStaticHelperFunctionsToCodeText('./manipulatedCode.php');
        require_once('./manipulatedCodeAddedStatic.php');
        return true;
    }

    public function fetchDataFromAjax()
    {
        return $_REQUEST['text'];
    }

    public function getArrayFromFile($file)
    {
        $arr = file($file, true);
        return $arr;
    }

    public function grepFunctionText($function, $file)
    {
        $arrWholeFile = $this->getArrayFromFile($file);
        $arrFunction = array();
        $lineOfFunctionStart = 0;
        $lineOfFunctionEnd = 0;
        $functionStartFound = false;
        $functionStartBracketFound = false;
        $functionEndBracketFound = false;
        $spaceOfFunctionStartBracket = 0;
        $functionEndFound = false;
        foreach ($arrWholeFile as $row) {
            if (!$functionStartFound) {
                $lineOfFunctionStart++;
                $lineOfFunctionEnd = $lineOfFunctionStart;
            } elseif(!$functionEndBracketFound) {
                $lineOfFunctionEnd++;
            }
            $functionMatch = '/function ' . $function . '/';
            if (0 < preg_match($functionMatch, $row)) {
                $arrFunction[] = $row;
                $functionStartFound = true;
            } elseif ($functionStartFound && !$functionStartBracketFound) {
                $arrFunction[] = $row;
                if (false !== strpos($row, "{")) {
                    $spaceOfFunctionStartBracket = strpos($row, "{");
                    $functionStartBracketFound = true;
                }
            } elseif ($functionStartBracketFound && !$functionEndBracketFound) {
                if ($spaceOfFunctionStartBracket ==  strpos($row, "}")) {
                    $functionEndBracketFound = true;
                }
                $arrFunction[] = $row;
            }
        }
        $arrFunction = $arrFunction;
        
        $functionEndStartPointArray = array();
        $functionEndStartPointArray[$function]['start'] = $lineOfFunctionStart-1;
        $functionEndStartPointArray[$function]['end'] = $lineOfFunctionEnd;
        $functionEndStartPointArray = $functionEndStartPointArray;
        $this->_functionCodeStartEndPoints = $functionEndStartPointArray;
        return $arrFunction;
    }

    public function replaceFunctionInCode($functionArray, $function, $file)
    {
        $arrWholeFile = $this->getArrayFromFile($file);
        $arrWholeFileLen = count($arrWholeFile);
        $functionContent = $functionArray[$function];
        $replacedFunctionInCode = array();
        $counter = 0;
        $counterInsertFunction = 0;
        $functionStartLine = $this->_functionCodeStartEndPoints[$function]['start'];
        $functionEndLine = $this->_functionCodeStartEndPoints[$function]['end'];
        $functionStartFound = false;
        $functionEndFound = false;
        $row = '';
        $row = '';
        for ($counter = 0; $counter < $arrWholeFileLen; $counter++) {
            if (!$functionStartFound && $counter == $functionStartLine) {
                $functionStartFound = true;
            } elseif (!$functionEndFound && $counterInsertFunction == count($functionContent)) {
                $functionEndFound = true;
                $counter = $functionEndLine;
            }
            if (!$functionStartFound) {
                $row = $arrWholeFile[$counter];
            } elseif(!$functionEndFound) {
                $row = $functionContent[$counterInsertFunction];
                $counterInsertFunction++;
//                error_log("\nwith row = " . $row);
            } else {
                $row = $arrWholeFile[$counter];
            }
            $replacedFunctionInCode[] = $row;
        }
        return $replacedFunctionInCode;
    }

    /**
     *
     * @param array $functionTextArray
     * @param string $function
     * @return array
     */
    public function addOnStaticCallInFunction($functionTextArray, $function)
    {
        $addedStaticCallInFunction = array();
        $match = array();
        $counterLineInOriginalFunction = 0;
        foreach ($functionTextArray[$function] as $row) {
            $addedStaticCallInFunction[] = $row;
            if (0 < preg_match('/\$\w+ = /', $row)) {
                preg_match('/\$(\w+) = /', $row, $match);
                $lineInCode = $this->_functionCodeStartEndPoints[$function]['start'] + $counterLineInOriginalFunction;
                $addedStaticCallInFunction[] = 'handleArray('
                    . 'getFunctionName(), get_defined_vars(), '
                    . $lineInCode . ', ' . $match[1]
                    . ");\n"
                ;
            }
            $counterLineInOriginalFunction++;
        }
        return $addedStaticCallInFunction;
    }

    public function appendStaticHelperFunctionsToCodeText($file)
    {
        $manipulatedFile = file_get_contents($file);
        $appendToFile = file_get_contents('appendToFileImproved.txt');
        $manipulatedFile .= $appendToFile;
        file_put_contents('manipulatedCodeAddedStatic.php', $manipulatedFile);
    }

    /**
     *
     * @param string $content
     */
    public function putTextInToFile($content, $file)
    {
        if (!empty($content)) {
            file_put_contents($file, $content);
        }
    }

    public function sendDataToAjax()
    {
    }

    public function fetchFunctionData()
    {

    }

    public function prepareDebugData()
    {
    }

    public function applyDebugDataToFunction()
    {

    }

    public function fillStringWithSpecificEscapeSigns()
    {

    }

    public function getFunctionData()
    {
    }

    public function getWholeAjaxContent()
    {
        if (!$this->_wholeAjaxContent) {
            $this->_wholeAjaxContent = '';
        }
        return $this->_wholeAjaxContent;
    }

    public function getAt() {
        if (!$this->_at) {
            $this->_at = Controller::getActionTemplate();
        }
        return $this->_at;
    }


    //put your code here
}

$codeToGui = new CodeToGui();
$codeToGui->mainRoutine();
